﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using System.Data;
using TRPOP.Common;
using TRPOP.Config;
using TRPOP.CMS.Message.model;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Message.DataAccess
{
    public class MessageDAL
    {

        private static TRPOP.ComSqlDAL.ICommonDAL CommonDAL = TRPOP.CMS.Plugin.ICommonDALAccess.GetInstance();
        #region 私有的方法
        ///	<summary>
        ///	从Table取数据到实体（留言实体）
        ///	</summary>
        ///	<param name="oDataRow">数据行</param>
        ///	<returns>返回实体</returns>
        private static MessageInfo ParseRowTomodel(DataRow oDataRow)
        {
            MessageInfo oInfo = new MessageInfo();
            oInfo.iMessageStatusID = oDataRow["iMessageStatusID"] is DBNull ? 0 : (int)oDataRow["iMessageStatusID"];
            oInfo.iMessageTypeID = oDataRow["iMessageTypeID"] is DBNull ? 0 : (int)oDataRow["iMessageTypeID"];
            oInfo.nvcMessageTitle = oDataRow["nvcMessageTitle"] is DBNull ? "" : (string)oDataRow["nvcMessageTitle"];
            oInfo.ntMessageContent = oDataRow["ntMessageContent"] is DBNull ? "" : (string)oDataRow["ntMessageContent"];
            oInfo.dtCreationTime = oDataRow["dtCreationTime"] is DBNull ? (new DateTime(1900, 1, 1)) : (DateTime)oDataRow["dtCreationTime"];
            oInfo.iMessageSend = oDataRow["iMessageSend"] is DBNull ? 0 : (int)oDataRow["iMessageSend"];
            oInfo.nvcMessageSend = oDataRow["nvcMessageSend"] is DBNull ? "" : (string)oDataRow["nvcMessageSend"];
            oInfo.nvcContactName = oDataRow["nvcContactName"] is DBNull ? "" : (string)oDataRow["nvcContactName"];
            oInfo.iContactGender = oDataRow["iContactGender"] is DBNull ? 1 : oDataRow["iContactGender"].ToString().ToInt();
            oInfo.nvcContactPhone = oDataRow["nvcContactPhone"] is DBNull ? "" : (string)oDataRow["nvcContactPhone"];
            oInfo.nvcContactMobile = oDataRow["nvcContactMobile"] is DBNull ? "" : (string)oDataRow["nvcContactMobile"];
            oInfo.nvcContactAddress = oDataRow["nvcContactAddress"] is DBNull ? "" : (string)oDataRow["nvcContactAddress"];
            oInfo.nvcContactEmail = oDataRow["nvcContactEmail"] is DBNull ? "" : (string)oDataRow["nvcContactEmail"];
            oInfo.nvcContactPostCode = oDataRow["nvcContactPostCode"] is DBNull ? "" : (string)oDataRow["nvcContactPostCode"];
            oInfo.bIsDelete = oDataRow["bIsDelete"] is DBNull ? false : (bool)oDataRow["bIsDelete"];
            oInfo.ntExtendProperty = oDataRow["ntExtendProperty"] is DBNull ? "" : (string)oDataRow["ntExtendProperty"];
            oInfo.iMessageID = oDataRow["iMessageID"] is DBNull ? 0 : (int)oDataRow["iMessageID"];
            return oInfo;
        }

        ///	<summary>
        ///	从Table取数据到实体(留言及回复实体)
        ///	</summary>
        ///	<param name="oDataRow">数据行</param>
        ///	<returns>返回实体</returns>
        private static Message_MessageReplyInfo ParseMssageAndReply(DataRow oDataRow)
        {
            Message_MessageReplyInfo oInfo = new Message_MessageReplyInfo();
            //留言
            oInfo.iMessageStatusID = oDataRow["iMessageStatusID"] is DBNull ? 0 : (int)oDataRow["iMessageStatusID"];
            oInfo.iMessageTypeID = oDataRow["iMessageTypeID"] is DBNull ? 0 : (int)oDataRow["iMessageTypeID"];
            oInfo.nvcMessageTitle = oDataRow["nvcMessageTitle"] is DBNull ? "" : (string)oDataRow["nvcMessageTitle"];
            oInfo.ntMessageContent = oDataRow["ntMessageContent"] is DBNull ? "" : (string)oDataRow["ntMessageContent"];
            oInfo.dtCreationTime = oDataRow["dtCreationTime"] is DBNull ? (new DateTime(1900, 1, 1)) : (DateTime)oDataRow["dtCreationTime"];
            oInfo.iMessageSend = oDataRow["iMessageSend"] is DBNull ? 0 : (int)oDataRow["iMessageSend"];
            oInfo.nvcMessageSend = oDataRow["nvcMessageSend"] is DBNull ? "" : (string)oDataRow["nvcMessageSend"];
            oInfo.nvcContactName = oDataRow["nvcContactName"] is DBNull ? "" : (string)oDataRow["nvcContactName"];
            oInfo.iContactGender = oDataRow["iContactGender"] is DBNull ? 1 : oDataRow["iContactGender"].ToString().ToInt();
            oInfo.nvcContactPhone = oDataRow["nvcContactPhone"] is DBNull ? "" : (string)oDataRow["nvcContactPhone"];
            oInfo.nvcContactMobile = oDataRow["nvcContactMobile"] is DBNull ? "" : (string)oDataRow["nvcContactMobile"];
            oInfo.nvcContactAddress = oDataRow["nvcContactAddress"] is DBNull ? "" : (string)oDataRow["nvcContactAddress"];
            oInfo.nvcContactEmail = oDataRow["nvcContactEmail"] is DBNull ? "" : (string)oDataRow["nvcContactEmail"];
            oInfo.nvcContactPostCode = oDataRow["nvcContactPostCode"] is DBNull ? "" : (string)oDataRow["nvcContactPostCode"];
            oInfo.bIsDelete = oDataRow["bIsDelete"] is DBNull ? false : (bool)oDataRow["bIsDelete"];
            oInfo.ntExtendProperty = oDataRow["ntExtendProperty"] is DBNull ? "" : (string)oDataRow["ntExtendProperty"];
            oInfo.iMessageID = oDataRow["iMessageID"] is DBNull ? 0 : (int)oDataRow["iMessageID"];
            //回复留言
            oInfo.iMessageReplyID = oDataRow["iMessageReplyID"] is DBNull ? 0 : (int)oDataRow["iMessageReplyID"];
            oInfo.nvcMessageReplyTitle = oDataRow["nvcMessageReplyTitle"] is DBNull ? "" : (string)oDataRow["nvcMessageReplyTitle"];
            oInfo.ntMessageReplyContent = oDataRow["ntMessageReplyContent"] is DBNull ? "" : (string)oDataRow["ntMessageReplyContent"];
            oInfo.nvcMessageReplyUserName = oDataRow["nvcMessageReplyUserName"] is DBNull ? "" : (string)oDataRow["nvcMessageReplyUserName"];
            oInfo.dtMessageReplyTime = oDataRow["dtMessageReplyTime"] is DBNull ? (new DateTime(1900, 1, 1)) : (DateTime)oDataRow["dtMessageReplyTime"];

            return oInfo;
        }

        ///	<summary>
        ///	通用的添加存储过程的变量
        ///	</summary>
        ///	<param name="osdDataBase">SqlDatabase</param>
        ///	<param name="odbCommand">DbCommand</param>
        ///	<param name="oInfo">实体</param>
        private static MessageInfo ParseModeltoParam(SqlDatabase odbDataBase, DbCommand odbCommand, MessageInfo oInfo)
        {
            odbDataBase.AddInParameter(odbCommand, "@iMessageStatusID", System.Data.DbType.Int32, oInfo.iMessageStatusID);
            odbDataBase.AddInParameter(odbCommand, "@iMessageTypeID", System.Data.DbType.Int32, oInfo.iMessageTypeID);
            odbDataBase.AddInParameter(odbCommand, "@nvcMessageTitle", System.Data.DbType.String, oInfo.nvcMessageTitle);
            odbDataBase.AddInParameter(odbCommand, "@ntMessageContent", System.Data.DbType.String, oInfo.ntMessageContent);
            odbDataBase.AddInParameter(odbCommand, "@dtCreationTime", System.Data.DbType.DateTime, oInfo.dtCreationTime);
            odbDataBase.AddInParameter(odbCommand, "@iMessageSend", System.Data.DbType.Int32, oInfo.iMessageSend);
            odbDataBase.AddInParameter(odbCommand, "@nvcMessageSend", System.Data.DbType.String, oInfo.nvcMessageSend);
            odbDataBase.AddInParameter(odbCommand, "@nvcContactName", System.Data.DbType.String, oInfo.nvcContactName);
            odbDataBase.AddInParameter(odbCommand, "@iContactGender", System.Data.DbType.Int32, oInfo.iContactGender);
            odbDataBase.AddInParameter(odbCommand, "@nvcContactPhone", System.Data.DbType.String, oInfo.nvcContactPhone);
            odbDataBase.AddInParameter(odbCommand, "@nvcContactMobile", System.Data.DbType.String, oInfo.nvcContactMobile);
            odbDataBase.AddInParameter(odbCommand, "@nvcContactAddress", System.Data.DbType.String, oInfo.nvcContactAddress);
            odbDataBase.AddInParameter(odbCommand, "@nvcContactEmail", System.Data.DbType.String, oInfo.nvcContactEmail);
            odbDataBase.AddInParameter(odbCommand, "@nvcContactPostCode", System.Data.DbType.String, oInfo.nvcContactPostCode);
            odbDataBase.AddInParameter(odbCommand, "@bIsDelete", System.Data.DbType.Boolean, oInfo.bIsDelete);
            odbDataBase.AddInParameter(odbCommand, "@ntExtendProperty", System.Data.DbType.String, oInfo.ntExtendProperty);
            odbDataBase.AddParameter(odbCommand, "@iMessageID", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, oInfo.iMessageID);
            return oInfo;
        }

        #endregion

        ///	<summary>
        ///	 更新或添加
        ///	</summary>
        ///	<param name="oInfo"></param>
        ///	<returns>bool</returns>
        public static bool Save(MessageInfo oInfo)
        {
            bool flag = false;
            try
            {
                SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DbCommand odbCommand = odbDataBase.GetStoredProcCommand("Message_Save");
                ParseModeltoParam(odbDataBase, odbCommand, oInfo);
                odbDataBase.ExecuteNonQuery(odbCommand);
                oInfo.iMessageID = (int)odbDataBase.GetParameterValue(odbCommand, "@iMessageID");
                flag = true;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return flag;
        }

        ///	<summary>
        ///	根据Primary Key获取信息实体
        ///	</summary>
        ///	<param name="intiMessageID"></param>
        ///	<returns>MessageInfo</returns>
        public static MessageInfo GetInfoById(int intiMessageID)
        {
            MessageInfo oInfo = null;
            string strSqlString = "SELECT * FROM Message WHERE  iMessageID=" + intiMessageID + "    ";
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSqlString, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                if (oDataTable.Rows.Count == 1)
                {
                    oInfo = ParseRowTomodel(oDataTable.Rows[0]);
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }

        ///	<summary>
        ///	 根据Primary Key删除信息实体
        ///	</summary>
        ///	<param name="intiMessageID"></param>
        ///	<returns>bool</returns>
        public static bool DeleteInfoById(int intiMessageID)
        {
            string strSqlString = "DELETE * FROM Message WHERE   iMessageID=" + intiMessageID + "    ";
            try
            {
                return CommonDAL.ExeSQLQuery(strSqlString, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection) == Common.EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
        }

        ///	<summary>
        ///	 获取信息列表（无分页）
        ///	</summary>
        ///	<param name="intTopn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="strCondition">自定义条件，要以And开头</param>
        ///	<param name="strOrderby">排序，不需要Order by </param>
        ///	<returns>List<MessageInfo></returns>
        public static List<MessageInfo> GetListByCondition(int intTopn, string strCondition, string strOrderby)
        {
            List<MessageInfo> oList = new List<MessageInfo>();
            string strSql = CommonDAL.strbuildSqlForList(intTopn, strCondition, strOrderby, "Message");
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSql, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }

        ///	<summary>
        ///	获取分页信息
        ///	</summary>
        /// <param name="intPageNumber">当前页页码</param>
        /// <param name="intPageSize">分页大小</param>
        /// <param name="strCondition">查询条件 and ....</param>
        /// <param name="strOrderby">排序的字段名 id desc</param>
        /// <param name="intRecordCount">总记录数</param>
        ///	<returns>List<MessageInfo></returns>
        public static List<MessageInfo> GetListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            List<MessageInfo> oList = new List<MessageInfo>();
            try
            {
                DataSet oDataSet = CommonDAL.GetListByPager("Message", "*", intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }

        public static List<Message_MessageReplyInfo> GetMessage_MessageReplyInfoListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            List<Message_MessageReplyInfo> oList = new List<Message_MessageReplyInfo>();

            try
            {
                DataSet oDataSet = CommonDAL.GetListByPager("vwMessage_MessageReplyInfo", "*", intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount, SystemInfoHelper.getSystemInfo().CMSDataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseMssageAndReply(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }
    }

}
